﻿Imports System.Data.Linq
Imports System.Linq

Public Class CUserController
    Dim objDataContext As TanNamChinhDataContext

    Public Function SelectAllRecord() As IQueryable(Of tblUser)
        Try
            Return (From u In objDataContext.tblUsers Where u.IsActive = True Select u)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function SelectRecordByID(ByVal _id As Integer) As tblUser
        Try
            Return (From u In objDataContext.tblUsers
                    Where u.ID_User = _id
                    Select u).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblUser) As Integer
        Try
            LocalTable.IsActive = True
            objDataContext.tblUsers.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            Return LocalTable.ID_User
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblUser)
        Try
            objDataContext.tblUsers.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblUser)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            objDataContext.tblUsers.DeleteOnSubmit( _
              (From u In objDataContext.tblUsers _
               Where u.ID_User = _id).Single)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub DeleteRecord2(ByVal LocalTable As tblUser)
        Try
            objDataContext.tblUsers.Attach(LocalTable, False)
            objDataContext.tblUsers.DeleteOnSubmit(LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Function CheckUserExists(ByVal sUserName As String)
        'cach 1
        Return objDataContext.tblUsers.Any(Function(u) u.UserName = sUserName And u.IsActive = True)
        '
    End Function
    Public Function CheckUser(ByVal sUseName As String, ByVal sPassword As String) As Boolean
        Dim _user = (From u In objDataContext.tblUsers Where u.UserName = sUseName And u.IsActive = True Select u).Single
        Return (CThanh_Utilities.verifyMd5Hash(sPassword, _user.Password))
    End Function

    Public Sub New()
        objDataContext = New TanNamChinhDataContext
    End Sub
    Public Sub New(ByVal _obj As TanNamChinhDataContext)
        objDataContext = _obj
    End Sub
End Class
